import {ref, reactive} from 'vue'
import {Back, NavTo} from '../../utlis/uni_api'
import * as $http from '@/utlis/interface/Login'
import {Public} from '@/Pinia/index'
import {onLoad, onShow, onUnload} from "@dcloudio/uni-app";

export default (proxy: any) => {
    // #ifdef APP
    // let platform = uni.getSystemInfoSync().platform;
    // if (platform === 'android') {
    // var jpushModule = uni.requireNativePlugin("JG-JPush")
    // }
    // #endif

    const {Publics} = Public()
    // 登录信息
    const Users = reactive({
        mobile: '',
        password: '',
        code: '',
        scene: '1',
        socialType: 10,
        status: 1,
        socialCode: '1024',
        socialState: '9b2ffbc1-7425-4155-9894-9d5c08541d62',
        socialCodeValid: true,
        deviceToken: "",
    })

    // 验证码
    let tips = ref('获取验证码') //默认btn文字
    let seconds = ref(60)
    // 获取验证码
    const getCode = () => {
        let rege = /^1[0-9]{10}$/
        if (!rege.test(Users.mobile.trim()))
            return proxy.$API(proxy).Toast('请输入正确的手机号', 'error')
        if (proxy.$refs.uCode.canGetCode) {
            uni.showLoading({
                title: '正在获取验证码',
            })
            $http.getPhoneCode(Users, proxy)
            setTimeout(() => {
                uni.hideLoading()
                // 这里此提示会被this.start()方法中的提示覆盖
                proxy.$API(proxy).Toast('验证码已发送', 'success')
                // 通知验证码组件内部开始倒计时
                proxy.$refs.uCode.start()
            }, 2000)
        } else {
            proxy.$API(proxy).Toast('倒计时结束后再发送', 'error')
        }
    }

    const end = () => {
        // uni.$u.toast('倒计时结束');
    }
    const start = () => {
        // uni.$u.toast('倒计时开始');
    }
    // 每秒触发一次 倒计时提示信息
    const codeChange = (text: string) => {
        tips.value = text
    }

    // 协议按钮
    const checkboxValue: any = ref([])
    const checkboxChange = (value: never[]) => {
        checkboxValue.value = value
    }

    const login_show = ref(false) //true验证码 false密码
    // 登录
    const login = async () => {
        if (!Users.mobile.trim()) {
            return proxy.$API(proxy).Toast('手机号不能为空', 'error')
        }
        let rege = /\D/g
        if (rege.test(Users.mobile.trim())) {
            return proxy.$API(proxy).Toast('请输入正确的手机号', 'error')
        }
        if (!Users.code.trim() && login_show.value) {
            return proxy.$API(proxy).Toast('验证码不能为空', 'error')
        }
        if (!Users.password.trim() && !login_show.value) {
            return proxy.$API(proxy).Toast('密码不能为空', 'error')
        }
        if (!checkboxValue.value[0]) {
            return isShowAgree.value = true
        }

        try {
            const rep: any = login_show.value
                ? await $http.loginByCode(Users, proxy)
                : await $http.loginByPassword(Users, proxy)
            if (rep) {
                // proxy.$API(proxy).Toast('登录成功', 'success')
                uni.setStorageSync('Authorization', 'Bearer ' + rep.data.accessToken)
                Publics.Authorization = 'Bearer ' + rep.data.accessToken
                setTimeout(() => {
                    uni.redirectTo({
                        url: '/pages/Home/Home',
                    })
                    //   Back()
                }, 1000)
            }
        } catch (err: any) {

            proxy.$API(proxy).Toast(err.msg, 'error')
        }
    }

    onLoad(() => {
        // if (platform === 'android') {
        // getRegistrationID()
        // }
        uni.removeStorageSync('Authorization')
    })

    onShow(() => {
        isShowAgree.value = false
    })

    const getRegistrationID = () => {
        // #ifdef APP
        // try {
        // 	jpushModule.getRegistrationID((result : any) => {
        // 		let registerID = result.registerID
        // 		Users.deviceToken = registerID
        // 		console.log("极光 getRegistrationID registerID = " + registerID)
        // 	})
        // } catch (e : any) {
        // 	console.log("login.ts getRegistrationID = ", e)
        // }
        // #endif
    }

    const isShowAgree = ref(false)
    const onAgreeClick = () => {
        checkboxValue.value[0] = true
        isShowAgree.value = false
        login()
    }

    return {
        isShowAgree,
        onAgreeClick,
        tips,
        getCode,
        codeChange,
        seconds,
        login,
        checkboxValue,
        checkboxChange,
        Users,
        login_show,


    }
}